package com.ld.hpfs.common.utils;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.xml.namespace.QName;

import com.ld.hpfs.common.printer.client.InvoiceStub;
import com.ld.hpfs.common.printer.client.Printer;
import com.ld.hpfs.common.printer.client.PrinterService;

public class GetPrintedStub {		
	private static final QName qName = new QName("http://service.printer.hps.com/", "PrinterService");
	public static ArrayList<HashMap<String,Object>> getPrintedStub(String clientIp){
		String clientIpPort = Const.PROPERTIES.getProperty("clientIpPort");
		ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();	
		if (clientIp != null) {
			try {
				PrinterService services = new PrinterService(new URL("http://"+clientIp +":"+clientIpPort+ "/IPrinter?wsdl"), qName);
				Printer port = services.getPrinterPort();	
				List<InvoiceStub> stubs = port.rtnPrintedStubs();	
							
				if(stubs.isEmpty()){//没有打印结果
					return null;
					//dealService.delectClientIp(clientIp);//把打印成功的ip在表中删除
				}else if(!stubs.isEmpty()){
					
					for (InvoiceStub invStub : stubs) {
						HashMap<String,Object> param = new HashMap<String,Object>();
						
						String HeadCode = invStub.getHeadCode();    //唯一标识
						String InvoiceCode = invStub.getInvoiceCode(); //发票代码
						if(InvoiceCode==null){
							InvoiceCode = "0";
						}
						String InvoiceNo = invStub.getInvoiceNo();   //发票号码	
						
						String date = invStub.getInvoiceDate(); //打印日期
						Date t = new SimpleDateFormat("yyyyMMddhhmmss").parse(date);
						date = new SimpleDateFormat("yyyyMMdd hh:mm:ss").format(t);
					
						String printer = invStub.getDrawer();      //打印人
						Short PrintStatus = invStub.getPrintStatus(); //打印状态  【0、成功打印； 1、打印失败】
						
						param.put("InvoiceCode", InvoiceCode);																	
						param.put("InvoiceNo", InvoiceNo);												
						param.put("HeadCode", HeadCode);
						param.put("PrintStatus", PrintStatus);	
						param.put("date", date);
						param.put("printer", printer);
						list.add(param);

					}											
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return list;
		}else{
			return null;
		}
		
		
	}
}
